"""
-------------------------------
- File_Name: config.py
- Date: 2025/2/7
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 项目配置、元数据及文档
-------------------------------
"""
import os
from dotenv import load_dotenv
from functools import lru_cache
from pydantic_settings import BaseSettings
from app.logging.config import log

# 加载 .env 文件中的环境变量
log.info("加载环境变量...")
load_dotenv()

# 文档描述
DESCRIPTION = """
## 为前端Vue3项目而搭建的FastAPI后端接口
**运维相关接口**
"""
class ApiEnvironmentVar(BaseSettings):
    """
    title: 网站标签名
    version: 版本
    summary: 简介
    description: 描述（非环境变量值，方便编写markdown格式）
    """
    title: str | None = None
    version: str | None = None
    summary: str | None = None
    description: str = DESCRIPTION

    # 项目数据库（支持orm）
    database: str | None = None
    # 其他数据库（使用非orm模式）
    other_oralce_01: str | None = None
    other_oralce_02: str | None = None

    # JWT
    secret_key: str | None = None
    algorithm: str | None = None
    access_token_expire_minutes: int | None = None
    refresh_token_expire_minutes: int | None = None

    # common
    default_password: str | None = None
    default_icon: str | None = None
    datetime_format: str| None = "%Y-%m-%d %H:%M:%S"

    # TORTOISE_ORM
    # tortoise_orm: dict | None = None
    class Config:
        env_file = f".env.{os.getenv('FASTAPI_ENV', 'development')}"

@lru_cache
def pull_settings():
    return ApiEnvironmentVar()


# 获取元数据
def get_settings() ->dict:
    """
    title: 网站标签名
    version: 版本
    summary: 简介
    root_path: URL前缀
    description: 描述（非环境变量值，方便编写markdown格式）
    :return: api文档元数据
    """
    setting = pull_settings()
    return {
        "title": setting.title,
        "version": setting.version,
        "root_path": f"/api/{setting.version}",
        "description": setting.description,
        "summary": setting.summary,
    }

# 获取所有环境变量
def get_api_environment_var():
    api_environment = pull_settings()
    return api_environment


"""tortoise-orm

"""
def get_tortoise_orm():
    api_environment = get_api_environment_var()
    tortoise_orm = {
        "connections": {
            "default": {
                "engine": "tortoise.backends.mysql",
                "credentials": {
                    "host": "127.0.0.1",
                    "port": "3306",
                    "user": "root",
                    "password": "root",
                    "database": "little-page-admin",
                    "minsize": 1,
                    "maxsize": 10,
                    "charset": "utf8mb4",
                    "echo": True
                }
            },
            "mysql": {
                "engine": "tortoise.backends.mysql",
                "credentials": {
                    "host": "127.0.0.1",
                    "port": "3306",
                    "user": "root",
                    "password": "root",
                    "database": "little-page-admin",
                    "minsize": 1,
                    "maxsize": 10,
                    "charset": "utf8mb4",
                    "echo": True
                }
            },
            "mysql2": {
                "engine": "tortoise.backends.mysql",
                "credentials": {
                    "host": "127.0.0.1",
                    "port": "3306",
                    "user": "root",
                    "password": "root",
                    "database": "lpa2",
                    "minsize": 1,
                    "maxsize": 10,
                    "charset": "utf8mb4",
                    "echo": True
                }
            },
            "sqlite": "sqlite://sqlite_db.sqlite3"
        },
        "apps": {
            "app_system": {
                "models": ["app.models", "aerich.models"],
                "default_connection": f"{api_environment.database}"
            }
        },
        "use_tz": False,
        "timezone": "Asia/Shanghai"
    }
    return tortoise_orm

TORTOISE_ORM = get_tortoise_orm()
